﻿using System;
using System.Collections.Generic;
using System.Data;

namespace MakeItWeb.Framework
{
    [Serializable]
    public class Photo : BusinessObject
    {
        private System.Guid m_guidPhotoId;
        private System.Guid m_guidPhotoalbumId;
        private string m_sFilenameTxt;
        private string m_sFilepathTxt;
        private long m_iFilesizeNum;
        private string m_sTitleTxt = string.Empty;
        private string m_sDescTxt = string.Empty;
        private System.DateTime m_dtCreateDttm;
        private bool m_bPublicInd = true;
        private bool m_bFeaturedInd = false;

        #region Public properties

        // tbl_photo: photo_id
        public virtual System.Guid PhotoId
        {
            get
            {
                return this.m_guidPhotoId;
            }
            set
            {
                if (!value.Equals(m_guidPhotoId))
                {
                    this.m_guidPhotoId = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: photoalbum_id
        public virtual System.Guid PhotoalbumId
        {
            get
            {
                return this.m_guidPhotoalbumId;
            }
            set
            {
                if (!value.Equals(m_guidPhotoalbumId))
                {
                    this.m_guidPhotoalbumId = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: filename_txt
        public virtual string FilenameTxt
        {
            get
            {
                return this.m_sFilenameTxt;
            }
            set
            {
                if (!value.Equals(m_sFilenameTxt))
                {
                    this.m_sFilenameTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: filepath_txt
        public virtual string FilepathTxt
        {
            get
            {
                return this.m_sFilepathTxt;
            }
            set
            {
                if (!value.Equals(m_sFilepathTxt))
                {
                    this.m_sFilepathTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: filesize_num
        public virtual long FilesizeNum
        {
            get
            {
                return this.m_iFilesizeNum;
            }
            set
            {
                if (!value.Equals(m_iFilesizeNum))
                {
                    this.m_iFilesizeNum = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: title_txt
        public virtual string TitleTxt
        {
            get
            {
                return this.m_sTitleTxt;
            }
            set
            {
                if (!value.Equals(m_sTitleTxt))
                {
                    this.m_sTitleTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: desc_txt
        public virtual string DescTxt
        {
            get
            {
                return this.m_sDescTxt;
            }
            set
            {
                if (!value.Equals(m_sDescTxt))
                {
                    this.m_sDescTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: create_dttm
        public virtual System.DateTime CreateDttm
        {
            get
            {
                return this.m_dtCreateDttm;
            }
            set
            {
                if (!value.Equals(m_dtCreateDttm))
                {
                    this.m_dtCreateDttm = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_photo: public_ind
        public virtual bool PublicInd
        {
            get
            {
                return this.m_bPublicInd;
            }
            set
            {
                if (!value.Equals(m_bPublicInd))
                {
                    this.m_bPublicInd = value;
                    this.SetDirty();
                }
            }
        }

        public virtual bool FeaturedInd
        {
            get { return m_bFeaturedInd; }
            set
            {
                if (!value.Equals(m_bFeaturedInd))
                {
                    m_bFeaturedInd = value;
                    SetDirty();
                }
            }
        }

        #endregion Public properties

        public override void Load(System.Data.DataRow dr)
        {
            if (DBNull.Value.Equals(dr["photo_id"]) == false)
            {
                m_guidPhotoId = ((System.Guid)(dr["photo_id"]));
            }
            if (DBNull.Value.Equals(dr["photoalbum_id"]) == false)
            {
                m_guidPhotoalbumId = ((System.Guid)(dr["photoalbum_id"]));
            }
            if (DBNull.Value.Equals(dr["filename_txt"]) == false)
            {
                m_sFilenameTxt = ((string)(dr["filename_txt"]));
            }
            if (DBNull.Value.Equals(dr["filepath_txt"]) == false)
            {
                m_sFilepathTxt = ((string)(dr["filepath_txt"]));
            }
            if (DBNull.Value.Equals(dr["filesize_num"]) == false)
            {
                m_iFilesizeNum = ((int)(dr["filesize_num"]));
            }
            if (DBNull.Value.Equals(dr["title_txt"]) == false)
            {
                m_sTitleTxt = ((string)(dr["title_txt"]));
            }
            if (DBNull.Value.Equals(dr["desc_txt"]) == false)
            {
                m_sDescTxt = ((string)(dr["desc_txt"]));
            }
            if (DBNull.Value.Equals(dr["create_dttm"]) == false)
            {
                m_dtCreateDttm = ((System.DateTime)(dr["create_dttm"]));
            }
            if (DBNull.Value.Equals(dr["public_ind"]) == false)
            {
                m_bPublicInd = ((bool)(dr["public_ind"]));
            }
            m_bFeaturedInd = (bool)dr["featured_ind"];
            this.SetLoaded();
        }

        public override void Save()
        {
            if (Dirty)
            {
                StoredProcArgs args = new StoredProcArgs();
                args["pPhotoId"] = m_guidPhotoId;
                args["pPhotoalbumId"] = m_guidPhotoalbumId;
                args["pFilenameTxt"] = m_sFilenameTxt;
                args["pFilepathTxt"] = m_sFilepathTxt;
                args["pFilesizeNum"] = m_iFilesizeNum;
                if (!string.IsNullOrEmpty(m_sTitleTxt))
                    args["pTitleTxt"] = m_sTitleTxt;
                if (!string.IsNullOrEmpty(m_sDescTxt))
                    args["pDescTxt"] = m_sDescTxt;
                args["pPublicInd"] = m_bPublicInd;
                args["pFeaturedInd"] = m_bFeaturedInd;
                GenericCud.ExecuteCud("proc_cm_SavePhoto", args);
                this.SetDirty(false);
            }
        }

        public static Photo[] ListPhoto(Guid guidPhotoalbumId)
        {
            List<Photo> lst = new List<Photo>();
            StoredProcArgs args = new StoredProcArgs();
            args["pPhotoalbumId"] = guidPhotoalbumId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListPhoto", args))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Photo ph = new Photo();
                    ph.Load(dr);
                    lst.Add(ph);
                }
            }
            return lst.ToArray();
        }

        public static Photo GetPhoto(Guid guidPhotoId)
        {
            Photo ph = new Photo();
            StoredProcArgs args = new StoredProcArgs();
            args["pPhotoId"] = guidPhotoId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetPhoto", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    ph.Load(ds.Tables[0].Rows[0]);
            }
            return ph;
        }

        public static void DeletePhoto(Guid guidPhotoId)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pPhotoId"] = guidPhotoId;
            GenericCud.ExecuteCud("proc_cm_DeletePhoto", args);
        }

        public static Photo[] ListFeaturedPhoto()
        {
            List<Photo> lst = new List<Photo>();
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListFeaturedPhoto", new StoredProcArgs()))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Photo ph = new Photo();
                    ph.Load(dr);
                    lst.Add(ph);
                }
            }
            return lst.ToArray();
        }

        public static Photo[] ListFeaturedPhotoAdmin()
        {
            List<Photo> lst = new List<Photo>();
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListFeaturedPhoto", new StoredProcArgs()))
            {
                DataView dv = ds.Tables[0].DefaultView;
                dv.Sort = "create_dttm ASC";
                foreach (DataRow dr in dv.ToTable().Rows)
                {
                    Photo ph = new Photo();
                    ph.Load(dr);
                    lst.Add(ph);
                }
            }
            return lst.ToArray();
        }
    }
}